package com.hengda.smart.speak.d.stc;

import android.os.Environment;

public class CommonHexUtil {

	/**
	 * byte数组转换成16进制字符数组
	 * @param src
	 * @return
	 */

	public static String[] bytesToHexStrings(byte[] src){

		if (src == null || src.length <= 0) {

			return null;

		}

		String[] str = new String[src.length];

		for (int i = 0; i < src.length; i++) {

			int v = src[i] & 0xFF;

			String hv = Integer.toHexString(v);

			if (hv.length() < 2) {

				str[i] = "0";

			}

			str[i] = hv;

		}

		return str;

	}


	/**

	 * byte数组转换成16进制字符串

	 * @param src

	 * @return

	 */

	public static String bytesToHexString(byte[] src){

		StringBuilder stringBuilder = new StringBuilder();

		if (src == null || src.length <= 0) {

			return null;

		}

		for (int i = 0; i < src.length; i++) {

			int v = src[i] & 0xFF;

			String hv = Integer.toHexString(v);

			if (hv.length() < 2) {

				stringBuilder.append(0);

			}

			stringBuilder.append(hv);

		}

		return stringBuilder.toString().toUpperCase();

	}


	/**
	 * wang zhi qiang
	 * @param hexString
	 * @return byte
	 */

	public static byte hexStringToBytes(String hexString) {

		if (hexString == null || hexString.equals("")) {
			return 0;
		}

		if(hexString.length()==1){
			hexString="0"+hexString;
		}
		hexString = hexString.toUpperCase();
		int length = hexString.length() / 2;
		char[] hexChars = hexString.toCharArray();
		byte[] d = new byte[length];
		for (int i = 0; i < length; i++) {
			int pos = i * 2;
			d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}
		return d[0];
	}

	public static byte charToByte(char c) {
		return (byte) "0123456789ABCDEF".indexOf(c);
	}


	public static byte[] getDoubleByteArray(int temInt) {
		byte[] tem = new byte[2];
		if (temInt > 0 && temInt < 65535) {
			if (temInt < 256) {
				tem[0] = (byte) 0x00;
				tem[1] = hexStringToBytes(Integer
						.toHexString(temInt));
			} else {
				String[] hexString = CommonHexUtil.get256HEX(temInt);
				tem[0]=hexStringToBytes(hexString[0]);
				tem[1]=hexStringToBytes(hexString[1]);
			}

		}else{
			tem[0]=(byte) 0x00;
			tem[1]=(byte) 0x00;
		}
		return tem;
	}

	public static byte getSingleByte(int temInt) {
		return hexStringToBytes(Integer.toHexString(temInt));
	}


	/**
	 *   获取
	 */
	public static String[]get256HEX(int no) {
		String[] hext = new String[2];
		if(no>255){
			hext[0]= Integer.toHexString(no/256);
			hext[1]= Integer.toHexString(no%256);
		}
		return hext;
	}


	/**
	 *
	 * @param buffer
	 * @return 获取十六进制
	 */

	public static String getHexStringByByteArray(byte[] buffer) {
		StringBuilder stringBuilder = new StringBuilder("");
		for (int i = 0; i < 22; i++) {
			int v = buffer[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString().toUpperCase();

	}


	public static boolean checkSDcard() {
		String status = Environment.getExternalStorageState();
		if (status.equals(Environment.MEDIA_MOUNTED)) {
			return true;
		} else {
			return false;
		}
	}


}
